package cn.bocweb.spinning.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import cn.bocweb.spinning.R;

/**
 * Created by chenchangxing on 16/3/7.
 */
public class WXUtil {


    public static boolean WXShareToFriend(String url, Context context) {

        IWXAPI iwxapi = WXAPIFactory.createWXAPI(context, Constant.WX_APP_ID, true);
        iwxapi.registerApp(Constant.WX_APP_ID);

        WXWebpageObject webapp = new WXWebpageObject();
        webapp.webpageUrl = url;

        WXMediaMessage msg = new WXMediaMessage(webapp);
        msg.title = "纺织大学堂";
        msg.description = "这里有系统的学习教材、海量的学习资料、合理的学习计划、一流的师资力量、专业的交流圈子，纺织大学堂app将为中国2000万纺织从业者提供继续教育的专业平台";
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.spinning_school);
        msg.thumbData = DeviceUtil.bmpToByteArray(bitmap, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("spinningschool");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;

        return iwxapi.sendReq(req);
    }

    public static boolean WXShareToFriend(String url, Context context, String title, String content) {

        IWXAPI iwxapi = WXAPIFactory.createWXAPI(context, Constant.WX_APP_ID, true);
        iwxapi.registerApp(Constant.WX_APP_ID);

        WXWebpageObject webapp = new WXWebpageObject();
        webapp.webpageUrl = url;

        WXMediaMessage msg = new WXMediaMessage(webapp);
        msg.title = title;
        msg.description = content;
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.spinning_school);
        msg.thumbData = DeviceUtil.bmpToByteArray(bitmap, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("spinningschool");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;

        return iwxapi.sendReq(req);
    }

    /**
     * 微信分享到朋友圈
     * @param url 网站地址
     */
    public static boolean WXShareToTimeline(String url, Context context) {
        IWXAPI iwxapi = WXAPIFactory.createWXAPI(context, Constant.WX_APP_ID, true);
        iwxapi.registerApp(Constant.WX_APP_ID);

        WXWebpageObject webapp = new WXWebpageObject();
        webapp.webpageUrl = url;

        WXMediaMessage msg = new WXMediaMessage(webapp);
        msg.title = "纺织大学堂";
        msg.description = "这里有系统的学习教材、海量的学习资料、合理的学习计划、一流的师资力量、专业的交流圈子，将为中国2000万纺织从业者提供继续教育的专业平台";
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.spinning_school);
        msg.thumbData = DeviceUtil.bitmap2Bytes(bitmap);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("spinningschool");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneTimeline;

        return iwxapi.sendReq(req);
    }

    public static boolean WXShareToTimeline(String url, Context context, String title, String content) {
        IWXAPI iwxapi = WXAPIFactory.createWXAPI(context, Constant.WX_APP_ID, true);
        iwxapi.registerApp(Constant.WX_APP_ID);

        WXWebpageObject webapp = new WXWebpageObject();
        webapp.webpageUrl = url;

        WXMediaMessage msg = new WXMediaMessage(webapp);
        msg.title = title;
        msg.description = content;
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.spinning_school);
        msg.thumbData = DeviceUtil.bitmap2Bytes(bitmap);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("spinningschool");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneTimeline;

        return iwxapi.sendReq(req);
    }

    private static String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}
